package bandidong.bean;

import bandidong.dao.MobileDAO;
import bandidong.dao.impl.MobileDAOImpl;
import bandidong.model.Mobile;
import bandidong.transfermodel.SearchMobileTransferModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless(mappedName = "mobileBeanRemote")
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
public class MobileBean implements MobileBeanRemote {
    
    @PersistenceContext(unitName = "persistenceUnit")
    EntityManager em;
    
    private MobileDAO mobileDAO = null;
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getAllMobiles() {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getAllMobiles();
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Mobile getMobileById(int id) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getMobileById(id);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String addMobile(Mobile mobile) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.addMobile(mobile);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String updateMobile(Mobile mobile) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.updateMobile(mobile);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String deleteMobile(Mobile mobile) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.deleteMobile(mobile);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getMobilesCriteria(SearchMobileTransferModel model, int page, int pageSize) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getMobilesCriteria(model, page, pageSize);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getMobilesCriteria(SearchMobileTransferModel model) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getMobilesCriteria(model);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkExists(String mobileName) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.checkExists(mobileName);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkExists(String mobileName, int mobileId) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.checkExists(mobileName, mobileId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getHotMobile(int maxSize) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getHotMobile(maxSize);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getHotMobile(int page, int pageSize) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getHotMobile(page, pageSize);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getPopularMobile(int maxSize, Date orderDate) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getPopularMobile(maxSize, orderDate);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Mobile> getPopularMobile(int page, int pageSize, Date orderDate) {
        mobileDAO = new MobileDAOImpl(em);
        return mobileDAO.getPopularMobile(page, pageSize, orderDate);
    }
}
